package com.jlastu.androiduidemo.views;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.jlastu.androiduidemo.R;
import com.jlastu.androiduidemo.adapter.CourseAdapter;
import com.jlastu.androiduidemo.base.BaseFragment;
import com.jlastu.androiduidemo.bean.Course;
import com.jlastu.androiduidemo.bean.Student;
import com.jlastu.androiduidemo.database.DBHelper;
import com.jlastu.androiduidemo.widget.EmptyView;
import com.jlastu.androiduidemo.widget.HeaderView;

import java.util.List;

public class CoursesFragment extends BaseFragment implements View.OnClickListener {

   private HeaderView mHeaderView;
   private ListView mLvCourses;
   private EmptyView mEmptyView;
   private List<Course> mCoursesList;
   private CourseAdapter mCourseAdapter;
   private AlertDialog mDialog;
   @Override
   protected int getLayoutId() {
      return R.layout.fragment_courses;
   }

   @SuppressLint("WrongViewCast")
   @Override
   protected void initView() {
      mHeaderView = mView.findViewById(R.id.header_view);
      mHeaderView.setTitle("我的课程");
      mLvCourses=mView.findViewById(R.id.lv_courses);
      mEmptyView=mView.findViewById(R.id.empty_view);

      mLvCourses.setEmptyView(mEmptyView);

      mCoursesList= DBHelper.getInstance(mContext).selectAllCourse();
      mCourseAdapter = new CourseAdapter(mCoursesList,mContext);
      mLvCourses.setAdapter(mCourseAdapter);

      mLvCourses.setOnItemClickListener(new AdapterView.OnItemClickListener() {
         @Override
         public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Course course =(Course) view.getTag(R.integer.coures_tag);
            DBHelper.getInstance(mContext).updateCourseNameById(course.getCourse_name()+"c",course.getCourse_id());
            refreshCourseList();
         }
      });

      mLvCourses.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
         @Override
         public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            Course course=(Course) view.getTag(R.integer.coures_tag);
            //弹框提示是否删除
            showDeleteDialog2(course);
            return true;
         }
      });
   }

   @Override
   protected void initListener() {mHeaderView.setRightClickListener(this);}
   public void onClick(View v){
      int id = v.getId();
      if (id == R.id.button_right){
         addCourse();
      }
   }
   private void showDeleteDialog(Course course){
      AlertDialog.Builder builder =new AlertDialog.Builder(mContext);
      builder.setTitle("删除");
      builder.setMessage("确定要删除吗？");
      builder.setIcon(R.drawable.attention);
      builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which) {

         }
      });
      builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which) {
            DBHelper.getInstance(mContext).deleteCourseByName(course.getCourse_name());
            showToast(course.getCourse_name() + "信息删除");
            refreshCourseList();
         }
      });
      Dialog dialog =builder.create();
      dialog.show();
   }

   private void showDeleteDialog2(Course course){
      LayoutInflater inflater = LayoutInflater.from(mContext);
      View v = inflater.inflate(R.layout.dialog_attention, null);
      TextView message = (TextView) v.findViewById(R.id.message);
      Button btn_sure = (Button) v.findViewById(R.id.button_right);
      Button btn_cancel = (Button) v.findViewById(R.id.button_left);
      message.setText("真的要删吗？");
      btn_sure.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            //确认删除，关闭弹框
            mDialog.cancel();
            //数据库中删除
            DBHelper.getInstance(mContext).deleteCourseByName(course.getCourse_name());
            showToast(course.getCourse_name() + "信息被删除");
            //删除后刷新列表
            refreshCourseList();
         }
      });
      btn_cancel.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            mDialog.cancel();
         }
      });
      AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
      mDialog = builder.create();
      //mDialog.setView(v);
      mDialog.show();
      Window window = mDialog.getWindow();
      //width 你想要的宽度； height 你想要的高度
      window.setLayout(840, 600);
      window.setContentView(v);
   }
   private void refreshCourseList(){
      mCoursesList = DBHelper.getInstance(mContext).selectAllCourse();
      mCourseAdapter.updateData(mCoursesList);
   }
   private void addCourse(){
      Log.d(TAG,"addCourses");

//      Course course = new Course();
//      course.setCourse_code("112");
//      course.setCourse_name("math");
//      course.setCourse_teacher("鹿野");
//      course.setLearning_hours("24");
//      mCoursesList.add(course);
//      DBHelper.getInstance(mContext).insertCourse(course);
//
//      Course course2 = new Course();
//      course2.setCourse_code("114");
//      course2.setCourse_name("English");
//      course2.setCourse_teacher("云辉");
//      course2.setLearning_hours("48");
//      mCoursesList.add(course2);
//      DBHelper.getInstance(mContext).insertCourse(course2);
      refreshCourseList();
   }
}
